package com.family.pedigree.service.impl;


import com.family.common.utils.StringUtils;
import com.family.pedigree.domain.FamilyProcessInstance;
import com.family.pedigree.mapper.FamilyProcessInstanceMapper;
import com.family.pedigree.service.IFamilyProcessInstanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 流程实例 服务实现类
 * </p>
 *
 * @author forever
 * @since 2024-11-24
 */
@Service
public class FamilyProcessInstanceServiceImpl extends ServiceImpl<FamilyProcessInstanceMapper, FamilyProcessInstance> implements IFamilyProcessInstanceService {

    @Override
    public List<?> getList(FamilyProcessInstance instance) {
        return lambdaQuery()
                .like(StringUtils.isNotBlank(instance.getProcessName()), FamilyProcessInstance::getProcessName, instance.getProcessName())
                .like(StringUtils.isNotBlank(instance.getTitle()), FamilyProcessInstance::getTitle, instance.getTitle())
                .eq(instance.getAuditStatus() != null, FamilyProcessInstance::getAuditId, instance.getAuditStatus())
                .orderByDesc(FamilyProcessInstance::getCreateTime)
                .list();
    }

    @Override
    public int send(FamilyProcessInstance instance) {
        Integer auditStatus = instance.getAuditStatus();
        FamilyProcessInstance temp = getById(instance.getId());
        //发起流程
        if (auditStatus == 1 && temp.getAuditStatus() == 0) {
            updateById(instance.setCreateBy("admin").setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now()));
            return 1;
        }
        //取消流程
        if (auditStatus == 0 && temp.getAuditStatus() == 1) {
            updateById(instance.setCreateBy("admin").setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now()));
            return 2;
        }
        return 0;
    }

    @Override
    public List<?> getWaitList(FamilyProcessInstance instance) {
        return lambdaQuery()
                .like(StringUtils.isNotBlank(instance.getProcessName()), FamilyProcessInstance::getProcessName, instance.getProcessName())
                .like(StringUtils.isNotBlank(instance.getTitle()), FamilyProcessInstance::getTitle, instance.getTitle())
                .eq(FamilyProcessInstance::getAuditId, 0)
                .list();
    }

    @Override
    public List<?> getDoneList(FamilyProcessInstance instance) {
        return lambdaQuery()
                .like(StringUtils.isNotBlank(instance.getProcessName()), FamilyProcessInstance::getProcessName, instance.getProcessName())
                .like(StringUtils.isNotBlank(instance.getTitle()), FamilyProcessInstance::getTitle, instance.getTitle())
                .in(instance.getAuditStatus() != null, FamilyProcessInstance::getAuditId, 1,2)
                .list();
    }

    @Override
    public int handle(FamilyProcessInstance instance) {
        instance.setAudit("admin").setAuditId(1L).setUpdateTime(LocalDateTime.now());
        updateById(instance);
        return 0;
    }
}
